// main.cpp
#include <iostream>
#include <cstdlib>
#include <string>

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <source_file.sy>\n";
        return 1;
    }
    std::string sourceFile = argv[1];
    std::string tokensFile = "tokens.txt";
    int ret;

    // 1. 调用 lexer.exe，而不是 Unix 风格的 ./lexer
    {
        std::string cmd = "lexer.exe " + sourceFile + " > " + tokensFile;
        std::cout << "[Main] Running lexer: " << cmd << "\n";
        ret = std::system(cmd.c_str());
        if (ret != 0) {
            std::cerr << "[Main] Lexer failed with exit code " << ret << "\n";
            return 1;
        }
        std::cout << "[Main] Tokens written to " << tokensFile << "\n";
    }

    // 2. 调用 parser.exe 处理 tokens.txt
    {
        std::string cmd = "parser.exe " + tokensFile;
        std::cout << "[Main] Running parser: " << cmd << "\n";
        ret = std::system(cmd.c_str());
        if (ret != 0) {
            std::cerr << "[Main] Parser failed with exit code " << ret << "\n";
            return 1;
        }
    }

    std::cout << "[Main] Compilation front-end completed successfully.\n";
    return 0;
}
